<?php
/**
 * 权限节点表
 * User: 王文凡<787802868@qq.com>
 * Date: 2016/7/21 0021
 * Time: 16:01
 */
namespace Common\Model;

use Think\Model;

class AuthRuleModel extends Model
{
    protected $_validate = [
        ['title', 'require', '标题必须填写！'],
        ['name', 'require', '链接必须填写！'],
        ['name', '', '模块标识已经存在！', 0, 'unique', 1]
    ];

    //新增节点
    public function ruleAdd($id = '')
    {
        if (!$this->create()) {
            $arr['state'] = 'error';
            $arr['info'] = $this->getError();
            return $arr;
        } else {
            if (empty($id)) {
                if ($this->add($_POST)) {
                    $arr['state'] = 'success';
                    $arr['info'] = '新增成功';
                } else {
                    $arr['state'] = 'error';
                    $arr['info'] = '新增失败';
                }
                return $arr;
            } else {
                //更新数据
                if ($this->where("id=%d", $id)->save($_POST)) {
                    $arr['state'] = 'success';
                    $arr['info'] = '修改成功';
                } else {
                    $arr['state'] = 'error';
                    $arr['info'] = '修改失败';
                }
                return $arr;

            }

        }

    }

    //查询节点列表
    public function ruleList($id = '')
    {
        if (empty($id)) {
            $counts = $this->count();
            $page = page($counts, 10, 5);
            $list = $this->limit($page->firstRow, $page->listRows)->select();
            $arr['list'] = $list;
            $arr['page'] = $page;
            return $arr;
        } else {
            return $this->where("id=%d", $id)->find();
        }
    }
}










